package org.chen.pool;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisConnect {

  private JedisPool pool;
  public JedisConnect() {
    GenericObjectPoolConfig config=new GenericObjectPoolConfig();
    //最大空闲数
    config.setMaxIdle(300);
    //最大连接数
    config.setMaxTotal(1000);
    //连接最大等待时间，如果是-1表示没有限制
    config.setMaxWaitMillis(30000);
    //在空闲时检查有效性
    config.setTestOnBorrow(true);

    pool=new JedisPool(config,"192.168.33.118");
  }

  public void execute(CallWithJedis call){
    try(Jedis jedis=pool.getResource()){
      call.call(jedis);
    }
  }
}
